﻿using psafth.FSR.Lapcounter.UserControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using psafth.FSR.Lapcounter.Sound;

namespace psafth.FSR.Lapcounter
{
    public partial class MainForm : Form
    {
        #region Private
        private DbEntities _db;
        private MulticastThread _multicastThread;

        #endregion

        public MainForm()
        {
            _db = new DbEntities();

            Splasher.Status = "Instansierar tråd för multicast";

            _multicastThread = new MulticastThread("239.0.0.222",2222);

            Splasher.Status = "Startar tråd för multicast";

            _multicastThread.RunWorkerAsync();

            foreach (Driver d in _db.Drivers)
            {
                Splasher.Status = string.Format("Läser in förare {0}", d.LicenceNumber);
                Splasher.ProgressMax = _db.Drivers.Count() + 2;
                Thread.Sleep(30);
            }

            InitializeComponent();

            Splasher.Close();
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        /// <summary>
        /// Use this to set page to be displayed
        /// </summary>
        public void SetPageToRace(Heat h)
        {
            ClearPanel(HoldingPanel);
            HoldingPanel.Controls.Add(new RaceControl(h, this));
        }

        private void ClearPanel(Panel p)
        {
            foreach (Control c in p.Controls)
                c.Dispose();

            p.Controls.Clear();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        public void SetAllButtons(bool enabled)
        {
            foreach (Control c in gbManage.Controls)
                c.Enabled = enabled;

            foreach (Control t in gbAdvanced.Controls)
                t.Enabled = enabled;

            foreach (Control s in gbRace.Controls)
                s.Enabled = enabled;
        }

        /// <summary>
        /// Send an object to the multicast stream to be received by any receivers.
        /// </summary>
        /// <param name="obj"></param>
        public void SendToMulticastThread(object obj, MulticastDataType type)
        {
            try
            {
                _multicastThread.Send(obj, type);
            }
            catch (Exception)
            {
                // Fetch everything.
            }
        }

        public bool SetStatusLabel(string s)
        {
            toolStripStatusLabel1.Text = "AMB Decoder status: " + s;

            return true;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void btnSettings_Click(object sender, EventArgs e)
        {
            ClearPanel(HoldingPanel);
            HoldingPanel.Controls.Add(new SettingsUC());
        }

        private void btnManageEvents_Click(object sender, EventArgs e)
        {
            ClearPanel(HoldingPanel);
            HoldingPanel.Controls.Add(new ManageEventsUC());
        }

        private void btnManageRaceClasses_Click(object sender, EventArgs e)
        {

        }

        private void btnManageDrivers_Click(object sender, EventArgs e)
        {
            ClearPanel(HoldingPanel);
            HoldingPanel.Controls.Add(new ManageDriversUC());
        }

        private void btn_quickstart_Click(object sender, EventArgs e)
        {
            ClearPanel(HoldingPanel);

            //Heat h = _db.HeatSet.First();

            HoldingPanel.Controls.Add(new RaceControl(null, this));
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Vill du avsluta? Eventuellt pågående race avslutas.", "Avsluta", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
